package zoro.shopping_list;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ShopListAdapter extends ArrayAdapter<Shoplist> {

	Context context;
	int resID;
	List<Shoplist> shoplists;
	MainActivity mainActivity;
	int count;
	
	public ShopListAdapter(Context context, int resID, List<Shoplist> shoplists) {
		super(context, resID, shoplists);

	this.context   = context;
	this.resID     = resID;
	this.shoplists = shoplists;
	mainActivity   = (MainActivity) context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = null;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(resID, null);
		
		final Shoplist sl = shoplists.get(position);
		
		ImageView imgIcon  = (ImageView) view.findViewById(R.id.img_icon);
		imgIcon.setBackgroundResource(sl.imgIcon);
		
		TextView  txtLabel = (TextView) view.findViewById(R.id.txt_label);
		txtLabel.setText(sl.label);
		
		TextView txtInfo   = (TextView) view.findViewById(R.id.txt_info);
		txtInfo.setText(sl.info);
		
		CheckBox check     = (CheckBox) view.findViewById(R.id.check_button);
		check.setChecked(sl.isChecked);
		check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
				sl.isChecked = isChecked;
				notifyDataSetChanged();
			}
		});
 		
		return view;
	}
}
